/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:15:44
 * @Last Modified by: songwh
 * @Last Modified time: 2018-04-11 17:36:13
*/

import moment from 'moment'

import Pages from '../../component/pages/pages.vue'
import Uploader from '../item-uploader/item-uploader.vue'

import { showMsg, getCastTalkList, deleteCastTalk, getBuyerList} from '../../vuex/action'

export default {
  name: 'cast-talk-item-list',
  components: {
    'vue-pages': Pages,
    'vue-uploader': Uploader
  },
  data() {
    return {
      list: [],
      search: {
        bqaid: '',
        keyword: ''
      },
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      buyerList: []
    }
  },
  vuex: {
    getters: {
      fresh: state => state.castTalkNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetlist: getCastTalkList,
      xDelete: deleteCastTalk,
      xGetBuyerList: getBuyerList
    }
  },
  created() {
    this.getList({})
    this.getBuyerList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      var params = {
        start: start,
        limit: limit
      }
      let {bqaid, keyword} = this.search
      if (bqaid) {
        params.bqaid = bqaid
      }
      if (keyword) {
        params.keyword = keyword
      }
      this.xGetlist(params)
        .then((res) => {
          this.list = res.data.msgSimpleResponses
          this.pages.totalPages = Math.ceil(res.data.total / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    showUploader(id) {
      var ref = this.$refs.uploader
      ref.init(id)
    },
    updateItem(item) {
      let ref = this.$root.$refs.itemConfig
      let {id,voiceTypeId,buyerQAId, voiceTotalSize, pubDate, voiceTotalSecond, title, tags,performances, newVoiceInfoDtos,meetingPith, guestQA, buyerQA,securitiesViewpoint,speakerInfos} = item

      pubDate = new Date(pubDate)
      pubDate = moment(pubDate).format('YYYY-MM-DD HH:mm:ss')
      let hour = Math.floor(voiceTotalSecond/3600)
      let min = Math.floor(voiceTotalSecond/60) % 60
      let sec = voiceTotalSecond % 60
      if(newVoiceInfoDtos &&newVoiceInfoDtos.length>0){
        ref.voicePicUrl = newVoiceInfoDtos[0].voicePicUrl
      }else{
        ref.voicePicUrl = ''
      }

      ref.form = {
        id: id,
        pubDate: pubDate,
        title: title,
        newVoiceInfoDtos: newVoiceInfoDtos,

        tags: tags.join(', '),
        speaker: speakerInfos[0].speaker,
        introduction: speakerInfos[0].introduction,

        meetingPith: meetingPith,
        securitiesViewpoint: securitiesViewpoint,
        guestQA: guestQA,
        buyerQA: buyerQA,
        voiceTypeId: voiceTypeId,
        buyerQAId: buyerQAId,
        voiceTotalSize: voiceTotalSize,
        voiceTotalSecond: voiceTotalSecond,
        performances: performances,
        time:{
          hour: hour,
          min: min,
          sec: sec
        }
      }
      ref.actionType = 'UPDATE'
      ref.showModal = true
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    searchList(){
      this.getList({
        start: 0
      })
    },
    getBuyerList({start=0, limit=1000}) {
      this.xGetBuyerList({
        start: start,
        limit: limit
      })
        .then((res) => {
          this.buyerList = res.data.list
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
